package com.ruoyi.apm.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.apm.model.HttpSequence;
import com.ruoyi.apm.service.HttpRequestService;
import com.ruoyi.apm.service.HttpSequenceService;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.tree.TreeDataUtils;
import com.ruoyi.common.utils.tree.TreeItem;
import org.apache.commons.lang.StringUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dock")
public class ApiMonitorController extends BaseController {

	@Autowired
	private HttpSequenceService httpSequenceService;
	
	@Autowired
	private HttpRequestService httpRequestService;

	/**
	 * 获取所有任务列表
	 * @param httpSequence
	 * @return
	 */
    @RequestMapping("/list")
    public TableDataInfo main(HttpSequence httpSequence) {
    	startPage();
		List<Map<String,Object>> list = httpSequenceService.getAllJob(httpSequence);
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(HttpStatus.SUCCESS);
		rspData.setMsg("查询成功");
		rspData.setRows(list);
		rspData.setTotal(httpSequenceService.getAllJobCount(httpSequence).get("total"));
		return rspData;
    }
    
    /*@RequestMapping("/monitorList")
    public String monitorList(ModelMap map, HttpServletRequest request) {
		String uuid = (String) request.getSession().getAttribute("authToken");
		List<Map<String,Object>> list = httpSequenceService.getMonitorList(uuid);
		map.addAttribute("monitorList", list);
    	return "monitor_list";
    }*/

	/**
	 * 获取执行日志
	 * @param guid
	 * @return
	 */
	@GetMapping("/excuteLog")
    public TableDataInfo monitorLog(String guid) {
    	startPage();
		List<Map<String,Object>> list = httpSequenceService.getLogByGuid(guid);
    	return getDataTable(list);
    }

	/**
	 * 获取接口调用日志
	 * @param id
	 * @return
	 */
    @GetMapping(value="/apiLog")
    public AjaxResult  apiLog(String id){
		List<Map<String, Object>> list = httpRequestService.getHttpRequestLogByPid(id);
        return AjaxResult.success(list);
    }

	/**
	 * 启动、停止任务
	 * @param guid
	 * @param enabled
	 * @return
	 */
    @RequestMapping(value="/enableMonitor",method=RequestMethod.GET)
    public AjaxResult  enableMonitor(String guid,String enabled){
		boolean b;
		if("true".equals(enabled)){
			b = httpRequestService.disableMonitor(guid);
		}else{
			b = httpRequestService.enableMonitor(guid);
		} 
        return AjaxResult.success(b);
    }

	/**
	 * 删除任务
	 * @param guid
	 * @return
	 */
    @RequestMapping(value="/deleteMonitor",method=RequestMethod.GET)
    public AjaxResult  deleteMonitor(String guid){
		boolean b = httpRequestService.deleteMonitor(guid);
		if(b){
			return AjaxResult.success(b);
		}else{
			return AjaxResult.error("失败");
		}
    }

    @GetMapping("/queryApiTreeData")
    public AjaxResult queryApiTreeData(){
    	List<TreeItem> list = httpSequenceService.queryTreeData();
		List<TreeItem> treeItem = TreeDataUtils.getInstance().enquireTree(list,"0");
    	return AjaxResult.success(treeItem);
	}
	
}
